package com.diamond.smart.info.service.impl;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diamond.common.utils.PageUtils;
import com.diamond.common.utils.Query;

import com.diamond.smart.info.dao.RegionDao;
import com.diamond.smart.info.entity.RegionEntity;
import com.diamond.smart.info.service.RegionService;
import org.springframework.util.StringUtils;


@Service("regionService")
public class RegionServiceImpl extends ServiceImpl<RegionDao, RegionEntity> implements RegionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<RegionEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength((String) params.get("key"))) {
            wrapper.eq("region_id", params.get("key"));
        }
        IPage<RegionEntity> page = this.page(
                new Query<RegionEntity>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 新增设备设备数量+1
     *
     * @param regionId
     */
    @Override
    public void updateDeviceCount(Long regionId) {
        RegionEntity regionEntity = this.getById(regionId);
        regionEntity.setDeviceCount(regionEntity.getDeviceCount() + 1);
        this.updateById(regionEntity);
    }

    /**
     * 设备弃用数量-1
     */
    @Override
    public void updateDeviceCountByUpdate(Long regionId) {
        RegionEntity regionEntity = this.getById(regionId);
        regionEntity.setDeviceCount(regionEntity.getDeviceCount() - 1);
        this.updateById(regionEntity);
    }

}